from django.conf import settings
from itsdangerous import TimedJSONWebSignatureSerializer


def dumps(data, expires):
    """加密"""
    serializer = TimedJSONWebSignatureSerializer(settings.SECRET_KEY, expires)
    result = serializer.dumps(data).decode()

    return result


def loads(data, expires):
    """解密"""
    serializer = TimedJSONWebSignatureSerializer(settings.SECRET_KEY, expires)
    try:
        # 因为openid是被当做键值对传入一个字典去加密的，取出来也是一个字典
        data_dict = serializer.loads(data)
        return data_dict
    except:
        # 抛异常的原因：超时
        return None
